package com.ple.app.importdata;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.ple.app.jzsf.R;
import com.ple.app.sevlet.GetFromServer;
import com.ple.app.sevlet.GetIdList;
import com.ple.app.sevlet.JsonTools;
import com.ple.app.sevlet.NetConnect;
import com.ple.app.suggest.Display_decisionActivity;
import com.ple.app.suggest.Gsetsuggest_Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Inputsoildata_Activity extends Activity {

    private ListView listView;
    private TextView dk_title;
    private String[] list_content={"ID","DK","area"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inputsoildata);
        if(NetConnect.isNetworkAvailable(Inputsoildata_Activity.this))update();
        inidata();
        updatalistview();
    }//oncreat
private void inidata(){
    listView= (ListView) findViewById(R.id.soil_data);
     dk_title= (TextView) findViewById(R.id.dk_title);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            HashMap<String,String> getmap= (HashMap<String, String>) listView.getItemAtPosition(position);
            String ID=getmap.get("ID");

            Intent intent=new Intent(Inputsoildata_Activity.this,Soilpoint_Activity.class);
            Bundle bundle=new Bundle();
            bundle.putString("DK",ID);
           intent.putExtras(bundle);
            startActivity(intent);

        }
    });
}
    /**
     * 更新Listview
     */
    private void updatalistview(){
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
       // String[] list_content={"ID","DK","area"};
        MyopenHelper myopenhelper=new MyopenHelper(Inputsoildata_Activity.this,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("DK",list_content,null,null,null,null,null,null);
        if(curor.getCount()>0){
            dk_title.setText("农场目前已存在的地块");
            while (curor.moveToNext()) {
                Map<String, Object> item1 = new HashMap<String, Object>();
                String id=curor.getString(0);
                String dk=curor.getString(1);
                String area="面积："+curor.getString(2)+"亩";
                item1.put("ID",id);
                item1.put("dk", dk);
                item1.put("area", area);
                list.add(item1);}

        }
        else{
            // TextView option= (TextView) findViewById(R.id.option);
            dk_title.setText("还没有已划定的地块");
        }
        curor.close();
        db.close();
        SimpleAdapter adapter = new SimpleAdapter(Inputsoildata_Activity.this, list, R.layout.soildk,
                new String[]{"ID","dk","area"},
                new int[]{R.id.dkid,R.id.name, R.id.area});
        //listView = (ListView) findViewById(R.id.seed_data); //(ListView) findViewById(R.id.seed_datagrid);
        listView.setAdapter(adapter);
    }
    /**
     * fuwu
     */
    private void update(){
        final Handler myHandler=new Handler() {
            @Override
            public void handleMessage(Message msg) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                String rponse= (String) msg.obj;
                if(rponse.isEmpty()){
                    //Toast.makeText(getApplicationContext(),"网络连接异常",Toast.LENGTH_LONG).show();
                }
                else if(rponse.equals("1")){
                    //Toast.makeText(getApplicationContext(),"同步完成",Toast.LENGTH_LONG).show();
                }
                else{
                    list= JsonTools.getListmap("data",rponse);//获得服务器传来的数据
                    int size=list.size();
                    MyopenHelper myopenhelper = new MyopenHelper(Inputsoildata_Activity.this, "seed.db", null, 1);
                    SQLiteDatabase db = myopenhelper.getReadableDatabase();

                    for(int i=0;i<size;i++){
                        Map<String, Object> item = new HashMap<String, Object>();
                        item= list.get(i);
                        String id= String.valueOf(item.get("id"));
                        String dk= String.valueOf(item.get("dk"));
                        String area= String.valueOf(item.get("area"));
                        /**
                         * 同步到本地
                         */
                        ContentValues values = new ContentValues();
                        values.put("ID", id);
                        values.put("DK", dk);
                        values.put("area", area);
                        db.insert("dk", null, values);
                    }// while(iterator.hasNext())
                    db.close();
                    updatalistview();


                }//else

            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                String jsonstring=GetIdList.getAllidFromTB(Inputsoildata_Activity.this,"dk");
                GetFromServer getfromserver=new GetFromServer();
                String result=getfromserver.dopost(jsonstring,null,getfromserver.dk_url);
                Message msg=new Message();
                msg.obj=result;
                myHandler.sendMessage(msg);
            }
        }).start();
    }
}
